package com.ruoyi.dispatch.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dispatch.domain.OnDutyReport;
import com.ruoyi.dispatch.enums.OnDutyReportCol;
import com.ruoyi.dispatch.mapper.OnDutyReportMapper;
import com.ruoyi.dispatch.service.IOnDutyReportService;
import com.ruoyi.framework.utils.BeanCopyUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 调度值班Service业务层处理
 * 
 * @author wuxiaofang
 * @date 2025-04-09
 */
@Service
public class OnDutyReportServiceImpl implements IOnDutyReportService 
{
    @Autowired
    private OnDutyReportMapper onDutyReportMapper;

    /**
     * 查询调度值班
     * 
     * @param dutyId 调度值班主键
     * @return 调度值班
     */
    @Override
    public OnDutyReport selectOnDutyReportByDutyId(Long dutyId)
    {
        return onDutyReportMapper.selectOnDutyReportByDutyId(dutyId);
    }

    /**
     * 查询调度值班列表
     * 
     * @param onDutyReport 调度值班
     * @return 调度值班
     */
    @Override
    public List<OnDutyReport> selectOnDutyReportList(OnDutyReport onDutyReport)
    {
        return onDutyReportMapper.selectOnDutyReportList(onDutyReport);
    }

    /**
     * 新增调度值班
     * 
     * @param onDutyReport 调度值班
     * @return 结果
     */
    @Override
    public int insertOnDutyReport(OnDutyReport onDutyReport)
    {
        onDutyReport.setCreateTime(DateUtils.getNowDate());
        return onDutyReportMapper.insertOnDutyReport(onDutyReport);
    }

    /**
     * 修改调度值班
     * 
     * @param onDutyReport 调度值班
     * @return 结果
     */
    @Override
    public int updateOnDutyReport(OnDutyReport onDutyReport)
    {
        onDutyReport.setUpdateTime(DateUtils.getNowDate());
        return onDutyReportMapper.updateOnDutyReport(onDutyReport);
    }

    /**
     * 批量删除调度值班
     * 
     * @param dutyIds 需要删除的调度值班主键
     * @return 结果
     */
    @Override
    public int deleteOnDutyReportByDutyIds(Long[] dutyIds)
    {
        return onDutyReportMapper.deleteOnDutyReportByDutyIds(dutyIds);
    }

    /**
     * 删除调度值班信息
     * 
     * @param dutyId 调度值班主键
     * @return 结果
     */
    @Override
    public int deleteOnDutyReportByDutyId(Long dutyId)
    {
        return onDutyReportMapper.deleteOnDutyReportByDutyId(dutyId);
    }

    @Override
    public List<Map<String, String>> handleColumnMap(String[] columnArray) {
        List<Map<String, String>> columns = Lists.newArrayList();
        for (String item : columnArray) {
            Map column = OnDutyReportCol.getByCode(item);
            if (column != null) {
                columns.add(column);
            }
        }
        return columns;
    }

    @Override
    public List<OnDutyReport> copyRecentlyMonthReport(String targetMonth) {
        String lastMonth = DateUtils.getlastMonth(targetMonth);

        LocalDate lastDayOfMonth = DateUtils.getLastDayOfMonth(targetMonth);

        //查询上月数据
        List<OnDutyReport> reportList = onDutyReportMapper.selectMonthDutyReport(lastMonth);
        List<OnDutyReport> newReportList = Lists.newArrayList();
        //处理上月数据，更新日期为目标月份
        for(OnDutyReport item : reportList) {
            //复制数据
            OnDutyReport newData = BeanCopyUtils.copyProperties(item,OnDutyReport.class);

            LocalDate sourceDate = item.getDutyDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            //月份加1
            LocalDate targetDate = sourceDate.plusMonths(1);
            newData.setDutyDate(Date.from(targetDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
            //修改必要字段
            newData.setDutyId(null);
            newData.setCreateTime(new Date());
            newData.setUpdateTime(null);
            newReportList.add(newData);
            //数据入库
            onDutyReportMapper.insertOnDutyReport(newData);
            //当月最后一天，退出循环
            if(targetDate.compareTo(lastDayOfMonth) == 0){
                break;
            }
        }
        return newReportList;
    }
}
